function debounce(fn, wait = 50, immediate) { 
  let timer = null 
  return function(...args) { 
      // this保存给context 
      const context = this 
      if (timer) clearTimeout(timer) 
     
     // immediate 为 true 表示第一次触发后执行 
     // timer 为空表示首次触发 
      if (immediate && !timer) { 
          fn.apply(context, args) 
      } 
      
      timer = setTimeout(() => { 
          fn.apply(context, args) 
      }, wait) 
  } 
}

export default debounce